﻿var refID;
$(function () {
    //初始化列表信息
    init();
    refID = $("#refID").val();
});

//初始化列表信息
function init() {
    //绑定树
    var treeDatas = [{ id: "7", text: "7天内到期的任务" }, { id: "30", text: "30天内到期的任务" }, { id: "", text: "所有任务" }];
    $('#tree').tree({
        animate: true,
        data: treeDatas,
        onClick: function (node) {
            doSearch();
        }
    });

    //绑定数据列表
    $('#list').datagrid({
        title: '',
        idField: 'TaskID',
        fitColumns: 'true',
        autoRowHeight: true,
        nowrap: true,
        striped: true,
        pagination: true,
        url: 'Task/GetList',
        columns: [[
			{ field: 'ck', checkbox: true },
			{ field: 'TaskID', title: 'TaskID', hidden: true },
			{ field: 'Title', title: '标题', width: 180 },
			{
			    field: 'FinishTime', title: '完成时间', width: 180,
			    formatter: function (value, row, index) {
			        return formatDate(value, "yyyy-MM-dd");
			    }
			},
			{ field: 'FinishPercent', title: '完成度(%)', width: 100 },
			{ field: 'CreateByName', title: '发布人', width: 180 },
			{
			    field: 'CreateTime', title: '发布时间', width: 180,
			    formatter: function (value, row, index) {
			        return formatDate(value, "yyyy-MM-dd");
			    }
			},
			{ field: 'ManageUserIDName', title: '负责人', width: 180 },

        ]],
    });
}

//弹出增加窗口
function add() {
    $('#dlg').dialog('open').dialog('setTitle', '增加');
    $('#fm').form('clear');
    $('#refID').val(refID);//把controller里传来的refID赋值到表单隐藏控件中，使提交表单时能带到controller
    $('#frmUploadFile').attr('src', 'Attachment/Index?refID=' + refID);
    url = '/Task/Create';
}

//弹出修改窗口
function edit() {
    var row = $('#list').datagrid('getSelected');
    if (row) {
        row["FinishTime"] = formatDate(row["FinishTime"], "yyyy-MM-dd");
        $('#fm').form('load', row);
        $('#dlg').dialog('open').dialog('setTitle', '修改');
        $('#frmUploadFile').attr('src', 'Attachment/Index?refID=' + row.TaskID);
        url = '/Task/Edit';
    }
}

//保存数据
function save() {
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            keditor.sync();// 编辑器值回写，这里的keditor 要和 div的  data-options 里建立的一样；
            return $(this).form('validate');
        },
        success: function (result) {
            if (result == 'OK') {
                $('#dlg').dialog('close');      // close the dialog  
                $('#list').datagrid('reload');    // reload the user data  
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result
                });
            }
        }
    });
}

//删除
function del() {
    var rows = $('#list').datagrid('getSelected');
    if (rows) {
        $.messager.confirm(delDlgTitle, delDlgMsg, function (r) {
            if (r) {
                $.post('/Task/Delete', { id: rows.TaskID },
                    function (result) {
                        if (result == 'OK') {
                            $("#list").datagrid('clearSelections');
                            $('#list').datagrid('reload');    // reload the user data  
                        } else {
                            $.messager.show({   // show error message  
                                title: 'Error',
                                msg: result
                            });
                        }
                    });
            }
        });
    }
}

//搜索
function doSearch() {
    //alert($("#QStatus").find("option:selected").text());
    var node = $('#tree').tree('getSelected');
    var innerDays = "0";
    if (node)
        innerDays = node.id;
    $('#list').datagrid('load', {
        innerDay: innerDays,
        Title: $('#Title').val(),
        Status: $('#QStatus').combobox('getValue')
    });
}